//
//  DFHomeVM.swift
//  DailyFresh
//
//  Created by Arthur on 2017/9/26.
//  Copyright © 2017年 DailyFresh. All rights reserved.
//

import UIKit
import SwiftyJSON

enum DFProductCellType {
    case none
    case banner
    case products
}

class DFHomeVM: NSObject {
    var sectionTypes: Array<DFProductCellType> = [.banner, .products]
    var bannerArray: Array<DFBannerModel> = []
    var productArray: Array<DFProductBaseModel> = []
    override init() {
        super.init()
        
    }
    
}

//MARK: - collectionView
extension DFHomeVM {
    
    public func sections() -> Int{
        return sectionTypes.count
    }
    
    public func type(section: Int) -> DFProductCellType {
        if section < sectionTypes.count {
            return sectionTypes[section]
        }
        
        return .none
    }
    
    public func rows(section: Int) -> Int {
        let type = self.type(section: section)
        switch type {
        case .none:
            return 0
        case .banner:
            return 1
            
        case .products:
            return productArray.count

        }
    }
    
    
    
    public func test() {
     
        let path = Bundle.main.path(forResource: "home", ofType: "json")
        let jsonData = NSData.init(contentsOfFile: path!)
        
        do {
            let json = JSON.init(data: jsonData! as Data)
            
            self._parseJson(json: json)
        }
    }
    
    fileprivate func _parseJson(json: JSON) {
        
        let dataJson = json["data"]
        
        let banners = dataJson["banner"].arrayValue
        
        banners.map({ (json) -> DFBannerModel in
            let banner = DFBannerModel.init(json: json)
            bannerArray.append(banner)
            return banner
        })
        
        let products = dataJson["products"].arrayValue
        
        products.map({ (json) -> DFProductBaseModel in
            
            let type = json["type"].stringValue
            
            let product: DFProductBaseModel
            
            if type == "group" {
                product = DFProductGroupModel.init(json: json)
            } else {
                product = DFProductModel.init(json: json)
            }
            
            productArray.append(product)
            return product
        })
    }
}


extension DFHomeVM {
    public func refetchData() {
        
    }
}
